var _MODE = 'dev';

//数据接口地址

//数据接口地址
let baseURL = {
	dev: 'http://47.99.60.65/api/dp/data',
	prod: '/api/dp/data',
} [_MODE];

let ajaxCall = (url, data, method, q) => {
	return new Promise((resolve, reject) => {
		$.ajax({
			url: q === true ? url : baseURL + url,
			data: data,
			contentType: "application/json;charset=UTF-8",
			type: method || 'get',
			dataType: "json",
			beforeSend: function (request) {
				request.setRequestHeader("Authorization", localStorage.getItem("token"));
			},
			success: function (ret) {
				resolve(ret)
			},
			error: function (err) {
				reject(err);
			}
		});
	});
}

ajaxCall("/api/house/checkLogin", {}, "get", true).then(ret => {
	if (ret.errorCode === "30101") {
		location.href = "/";
	}
});

let compe = (prop) => {
	return (obj1, obj2) => {
		let val1 = obj1[prop];
		let val2 = obj2[prop];
		if (!isNaN(val1)) {
			val1 = Number(val1);
		}
		if (!isNaN(val2)) {
			val2 = Number(val2);
		}
		if (val1 < val2) {
			return 1;
		} else if (val1 > val2) {
			return -1;
		} else {
			return 0;
		}
	}
}

function countPercentage(countArray, unit, precision) {
	var j = countArray.reduce(function (prev, next, index, array) {
		return prev + next;
	})
	var resultArray = [];
	precision = precision == 0 ? 0 : 2;
	for (var i = 0; i < countArray.length; i++) {
		if (j > 0) {
			var k = Math.floor((countArray[i] / j) * 100).toFixed(precision) + unit || "%";
			resultArray.push(k);
		} else {
			var k = Math.floor((1 / countArray.length) * 100).toFixed(precision) + unit || "%";
			resultArray.push(k);
		}
	}
	return resultArray;
}
